package servlet;

import java.io.IOException;
import org.bson.Document;
import org.bson.types.ObjectId;

import com.mongodb.client.MongoCursor;

import mongo.SchoolYearSemester;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(description = "schoolYearSemesterProcess", urlPatterns = { "/manager/schoolYearSemester/schoolYearSemester" })
public class schoolYearSemester extends HttpServlet{
	private SchoolYearSemester SchoolYearSemester = new SchoolYearSemester();
	HttpSession session = null;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=UTF-8");
		
		String method = request.getParameter("method");
		if(method.equals("add")){//如果已经有当前学期数据，则不允许添加，只能修改
			this.add(request, response);
		}else if(method.equals("update")){
			this.update(request, response);
		}else if(method.equals("get")){
			this.get(request, response);
		}else{
//			request.getRequestDispatcher("404.jsp").forward(request, response); 添加错误页面
		}
	
	}
	
	
	public void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Boolean result = null ;
		try{
				String year = request.getParameter("year");
				String semester = request.getParameter("semester");
				Document newDeparment = new Document("year",year).
						append("semester", semester);
				result = SchoolYearSemester.add(newDeparment);
				session = request.getSession();
				session.setAttribute("yearSemester", SchoolYearSemester.get());
				if(result) {
					this.get(request, response);
				}
			//TODO针对返回结果和所属模块，返回到相对应的页面
		}catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
            //TODO发生异常,跳转到对应页面
        }
		
	}
	
	
	public void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Boolean result = null;
		
        try {
    			ObjectId _id = new ObjectId(request.getParameter("_id"));
				String year = request.getParameter("year");
				String semester = request.getParameter("semester");
				Document newDeparment = new Document("year",year).
						append("semester", semester);
    			result = SchoolYearSemester.update(_id,newDeparment);
				session = request.getSession();
				session.setAttribute("yearSemester", SchoolYearSemester.get());
    			if(result) {
					this.get(request, response);
				}
    		//TODO针对返回结果和所属模块，返回到相对应的页面
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          //TODO发生异常,跳转到对应页面
        }
		
	}
	
	public void get(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
        try {
        	Document result = SchoolYearSemester.get();
    		request.setAttribute("result", result);
    	    request.getRequestDispatcher("schoolYearSemesterList.jsp").forward(request, response);
    		//TODO针对所属模块，返回到相对应的页面   
        }catch(Exception e){
            System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          //TODO发生异常,跳转到对应页面
        }

	}
	
}
